import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/index")
public class Index extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        HttpSession httpSession = req.getSession(false);
        if (httpSession == null){
            resp.sendRedirect("login.html");
            return;
        }
        String userId = (String) httpSession.getAttribute("userId");
        Integer loginCount = (Integer) httpSession.getAttribute("loginCount");
        loginCount = loginCount + 1;
        httpSession.setAttribute("loginCount",loginCount);

        StringBuilder html = new StringBuilder();
        html.append("<div> userId </div>" + userId);
        html.append(("<div> loginCount </div>" + loginCount));
        resp.getWriter().write(html.toString());
    }
}
